package com.tyj.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tyj.movie.constant.AjaxConstant;
import com.tyj.movie.entity.GiveLikeRelation;
import com.tyj.movie.entity.MovieComment;
import com.tyj.movie.entity.WatchNumberUserRelation;
import com.tyj.movie.mapper.GiveLikeRelationMapper;
import com.tyj.movie.mapper.MovieCommentMapper;
import com.tyj.movie.mapper.MovieMapper;
import com.tyj.movie.mapper.WatchNumberUserRelationMapper;
import com.tyj.movie.service.MovieCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyj.movie.service.WatchNumberUserRelationService;
import com.tyj.movie.util.R;
import com.tyj.movie.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Service
public class MovieCommentServiceImpl extends ServiceImpl<MovieCommentMapper, MovieComment> implements MovieCommentService {

    @Autowired
    MovieCommentService movieCommentService;

    @Autowired
    WatchNumberUserRelationMapper watchNumberUserRelationMapper;

    @Autowired
    MovieMapper movieMapper;

    @Autowired
    GiveLikeRelationMapper giveLikeRelationMapper;

    @Autowired
    MovieCommentMapper movieCommentMapper;

    /**
     * 添加评论
     * @param movieComment
     * @return
     */
    @Override
    public R addComment(MovieComment movieComment) {

        Long userId = SecurityUtil.getSessionUser().getId();
        Long movieId = movieComment.getMovieId();

        //判断评论是否存在
        MovieComment one = movieCommentService.getOne(new QueryWrapper<MovieComment>().eq("movie_id", movieId).eq("user_id", userId));
        if (one==null){
            //添加评论
            movieComment.setNum(0);
            movieComment.setCreateDate(new Date());
            movieComment.setUserId(userId);
            movieCommentService.save(movieComment);
        }else {
            //修改评论
            movieComment.setUserId(userId);
            movieComment.setId(one.getId());
            movieCommentService.updateById(movieComment);
        }


        //查看想看 评论后默认想看
        WatchNumberUserRelation numberUserRelation = watchNumberUserRelationMapper.selectOne(new QueryWrapper<WatchNumberUserRelation>().eq("user_id", userId).eq("movie_id",movieId));
        if (numberUserRelation == null || numberUserRelation.getState().equals(1)) {
            //未点击过
            //增加电影的想看数量
            movieMapper.updateWatchNumberAddByMovieId(movieId);
            //判断数据库是否有该数据
            if (numberUserRelation == null) {
                //增加关系表
                WatchNumberUserRelation watchNumberUserRelation = new WatchNumberUserRelation();
                watchNumberUserRelation.setMovieId(movieId);
                watchNumberUserRelation.setUserId(userId);
                watchNumberUserRelation.setState(0);
                watchNumberUserRelationMapper.insert(watchNumberUserRelation);
            } else {
                //修改关系表状态
                WatchNumberUserRelation watchNumberUserRelation = new WatchNumberUserRelation();
                watchNumberUserRelation.setMovieId(movieId);
                watchNumberUserRelation.setUserId(userId);
                watchNumberUserRelation.setState(0);
                watchNumberUserRelation.setId(numberUserRelation.getId());
                watchNumberUserRelationMapper.updateById(watchNumberUserRelation);
            }
        }
        return R.ok(movieComment);
    }

    /**
     * 点赞
     * @param id 评论id
     * @return
     */
    @Override
    @Transactional
    public R giveLike(Long id,Long movieId) {
        //判断是否点赞过
        Long userId = SecurityUtil.getSessionUser().getId();
        GiveLikeRelation giveLikeRelation = giveLikeRelationMapper.selectOne(new QueryWrapper<GiveLikeRelation>().eq("user_id", userId).eq("movie_comment_id", id));
        if (giveLikeRelation==null){
            //未点赞
                //增加点赞数
                movieCommentMapper.movieCommentNumAdd(id);
                //添加关联表
                GiveLikeRelation addObject=new GiveLikeRelation();
                addObject.setMovieCommentId(id);
                addObject.setUserId(userId);
                addObject.setMovieId(movieId);
                giveLikeRelationMapper.insert(addObject);
                return R.ok(AjaxConstant.AjAX_GIVE_LIKE_YES.getCode(),AjaxConstant.AjAX_GIVE_LIKE_YES.getMsg());
        }{
            //取消点赞
                //减少点赞数
                movieCommentMapper.movieCommentNumReduce(id);
                //删除关联表
                giveLikeRelationMapper.delete(new UpdateWrapper<GiveLikeRelation>().eq("user_id", userId).eq("movie_comment_id", id));
                return R.ok(AjaxConstant.AjAX_GIVE_LIKE_NO.getCode(),AjaxConstant.AjAX_GIVE_LIKE_NO.getMsg());

        }
    }
}
